{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3cc6dd6d",
   "metadata": {},
   "source": [
    "# 多Instance深度学习文件生成\n",
    "\n",
    "1. Bbox裁剪，生成多个ROI区域。\n",
    "2. 视频多帧，样本名称_帧ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "050b2358",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 类似于Data Pattern，数据目录\n",
    "root = r'E:\\20230802-Dental\\Dental X_Ray\\predict\\crop\\impacted tooth'\n",
    "# label文件，需要有group列，和label列以及ID列\n",
    "label_file = 'path2your.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "645b6bf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "def get_ID(x):\n",
    "    fname, ext = os.path.splitext(x)\n",
    "    return '_'.join(fname.split('_')[:-1]) + ext\n",
    "\n",
    "samples = pd.DataFrame(os.listdir(root), columns=['fname'])\n",
    "samples['ID'] = samples['fname'].map(lambda x: get_ID(x))\n",
    "samples"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4d8104c",
   "metadata": {},
   "source": [
    "# 匹配label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ac8fe03",
   "metadata": {},
   "outputs": [],
   "source": [
    "from onekey_algo.custom.utils import print_join_info\n",
    "\n",
    "label_data = pd.read_csv(label_file)\n",
    "print_join_info(samples, label_data)\n",
    "label_data = pd.merge(samples, label_data, on=\"ID\", how='inner')\n",
    "label_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "407e404f",
   "metadata": {},
   "source": [
    "# 分group保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a0aee4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for subset in set(label_data['group']):\n",
    "    subdata = label_data[label_data['group'] == subset]\n",
    "    subdata[['fname', 'label']].to_csv(f'{subset}.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
